//
//  RW_FeedbackViewController.m
//  SleepApp
//
//  Created by chuangqi on 2021/4/9.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_FeedbackViewController.h"

@interface RW_FeedbackViewController (){
    /** 意见类型 */
    NSString *_string_type;
}
@property (nonatomic,strong) UILabel *title_label;
@property (nonatomic,strong) UIButton *class_button;
@property (nonatomic,strong) UITextView *input_text_view;
@property (nonatomic,strong) UILabel *placeholder_label;

@end

@implementation RW_FeedbackViewController
+(void)pushFeedbackFromVc:(UIViewController *)fromVc{
    RW_FeedbackViewController *vc = [[RW_FeedbackViewController alloc]init];
    [fromVc.navigationController pushViewController:vc animated:YES];
}
- (void)initView{
    self.view.backgroundColor = HexColor(@"f6f6f6");
    [self.contentView addSubview:self.class_button];
    [self.contentView addSubview:self.input_text_view];
    [self.contentView addSubview:self.title_label];
    
    [self.class_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(10);
        make.right.mas_equalTo(self.contentView).mas_offset(-10);
        make.top.mas_equalTo(self.contentView).mas_offset(10);
        make.height.mas_offset(45);
    }];
    [self.input_text_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(10);
        make.right.mas_equalTo(self.contentView).mas_offset(-10);
        make.top.mas_equalTo(self.class_button.mas_bottom).mas_offset(10);
        make.height.mas_offset(150);
    }];
    [self.title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(10);
        make.right.mas_equalTo(self.contentView).mas_offset(-10);
        make.top.mas_equalTo(self.input_text_view.mas_bottom).mas_offset(10);
    }];
}
#pragma mark -- 功能性
-(void)click_select_feedback_type:(UIButton *)sender{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"选择提供意见类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"功能意见" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self changeFeedbackTypeString:@"功能意见"];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"使用意见" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self changeFeedbackTypeString:@"使用意见"];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"交互意见" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self changeFeedbackTypeString:@"交互意见"];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"BUG提交" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self changeFeedbackTypeString:@"BUG提交"];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"不知道啥分类" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self changeFeedbackTypeString:@"不知道啥分类"];
    }]];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:actionSheet animated:YES completion:nil];
}
-(void)changeFeedbackTypeString:(NSString *)string_type{
    _string_type = string_type;
    [self.class_button setTitle:_string_type forState:UIControlStateNormal];
}
#pragma mark -- 父控制器功能
-(NSString *)set_title{
    return @"意见建议";
}
-(UIButton *)set_rightButton{
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    return button;
}
-(void)right_button_event:(UIButton *)sender{
    if (!_string_type){
        [SVProgressHUD showError_wl:@"还木有选择意见类型呢!"];
        return;
    }
    if (!self.input_text_view.text.length){
        [SVProgressHUD showError_wl:@"好歹给点内容是吧，不然太过分了！"];
        return;
    }
    NSDictionary *dic = @{
        @"type":_string_type,
        @"content":self.input_text_view.text
    };
    [SVProgressHUD showMask_wl];
    [[[self pushFeedbackParams:dic] then:^id _Nullable(NSString * _Nullable value) {
        [SVProgressHUD dismiss_wl];
        [self.navigationController popViewControllerAnimated:YES];
        return nil;
    }] catch:^(NSError * _Nonnull error) {
        [SVProgressHUD showError_wl:error.domain];
    }];
}
#pragma mark -- 懒加载
-(UILabel *)title_label{
    if (!_title_label){
        _title_label = [[UILabel alloc]init];
        _title_label.textColor = HexColor(@"666666");
        _title_label.font = [UIFont systemFontOfSize:9 weight:UIFontWeightRegular];
        _title_label.numberOfLines = 0;
        _title_label.text = @"(•ૢ⚈͒⌄⚈͒•ૢ)洒家兼职了原生,后台,设计,产品,运维职位诞生的APP来讲，绝对有好多不人性化的设计与功能体验，感谢提供意见反馈！ଘ(੭ˊᵕˋ)੭";
    }
    return _title_label;
}
-(UIButton *)class_button{
    if (!_class_button){
        _class_button = [[UIButton alloc]init];
        [_class_button setTitle:@"点击选择类型" forState:UIControlStateNormal];
        [_class_button setTitleColor:HexColor(@"333333") forState:UIControlStateNormal];
        _class_button.backgroundColor = UIColor.whiteColor;
        _class_button.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
        _class_button.layer.cornerRadius = 8;
        _class_button.clipsToBounds = YES;
        [_class_button addTarget:self action:@selector(click_select_feedback_type:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _class_button;
}
-(UITextView *)input_text_view{
    if (!_input_text_view){
        _input_text_view = [[UITextView alloc]init];
        _input_text_view.backgroundColor = [UIColor whiteColor];
        _input_text_view.layer.cornerRadius = 8;
        _input_text_view.clipsToBounds = YES;
        _input_text_view.textColor = HexColor(@"333333");
        _input_text_view.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
        __WEAK_SELF_YLSLIDE
        [_input_text_view.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
            weakSelf.placeholder_label.hidden = x.length;
        }];
        [_input_text_view addSubview:self.placeholder_label];
        [self.placeholder_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_input_text_view).mas_offset(4.5f);
            make.top.mas_equalTo(_input_text_view).mas_offset(8);
        }];
    }
    return _input_text_view;
}
-(UILabel *)placeholder_label{
    if (!_placeholder_label){
        _placeholder_label = [[UILabel alloc]init];
        _placeholder_label.text = @"介个地方输入内容";
        _placeholder_label.textColor = HexColor(@"aaaaaa");
        _placeholder_label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    }
    return _placeholder_label;
}




#pragma mark -- 网络请求
/** 删除歌单歌曲歌曲信息 */
- (FBLPromise<NSString *> *)pushFeedbackParams:(NSDictionary *)params{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool putWithUrl:createFeedbackUrl refreshCache:NO params:params success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(model.data);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}
@end
